/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.mvc.controller.moment;

import com.workcell.workdata.im.annotation.CreateAction;
import com.workcell.workdata.im.annotation.UID;
import com.workcell.workdata.im.entity.Comment;
import com.workcell.workdata.im.model.request.CommentRequest;
import com.workcell.workdata.im.model.response.ResponseEntity;
import com.workcell.workdata.im.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/comment")
@Api(produces = "application/json", tags = "朋友圈评论接口" )
@Validated
public class CommentController {

	@Resource
	private CommentService commentService;

	@ApiOperation(httpMethod = "POST", value = "添加评论")
	@PostMapping(value = "")
	@Validated(CreateAction.class)
	public ResponseEntity<Long> publish(@ApiIgnore @UID Long uid, @Valid @RequestBody CommentRequest request) {

		Comment comment = request.ofComment();

		comment.setUid(uid);

		commentService.add(comment);

		return ResponseEntity.ok(comment.getId());
	}

	@ApiOperation(httpMethod = "POST", value = "点赞")
	@ApiImplicitParam(name = "momentId", value = "朋友圈内容ID", paramType = "path", dataTypeClass = Long.class,example = "0",required = true)
	@PostMapping(value = "/praise/{momentId}")
	public ResponseEntity<Long> praise(@ApiIgnore @UID Long uid, @PathVariable long momentId) {

		Comment comment = new Comment();
		comment.setUid(uid);
		comment.setMomentId(momentId);
		comment.setType(Comment.ACTION_PRAISE);

		commentService.add(comment);

		return ResponseEntity.ok(comment.getId());
	}



	@ApiOperation(httpMethod = "GET", value = "获取评论详情")
	@ApiImplicitParam(name = "id", value = "评论ID", paramType = "path", dataTypeClass = Long.class,example = "0",required = true)
	@GetMapping(value = "/{id}")
	public ResponseEntity<Comment> get(@PathVariable long id) {
		return ResponseEntity.ok(commentService.findOne(id));
	}

	@ApiOperation(httpMethod = "DELETE", value = "删除评论/取消点赞")
	@ApiImplicitParam(name = "id", value = "评论ID", paramType = "path", dataTypeClass = Long.class,example = "0",required = true)
	@DeleteMapping(value = "/{id}")
	public ResponseEntity<Void> delete(@ApiIgnore @UID Long uid, @PathVariable long id) {

		commentService.delete(id);

		return ResponseEntity.ok();
	}

}
